package com.jml.注册校验器;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.validation.Valid;

@Controller
@RequestMapping("/valid")
public class ValidController {

    @Autowired
    private User参数校验Validator validator;

    //这里就可以实现参数的额校验
    @InitBinder
    public void initBinder(WebDataBinder binder){
        //WebDataBinder有两个方法，
        // 一个是addValidators就是增加一个，一个是setValidators把原来的替换成现有的
        binder.addValidators(validator);
    }

    @RequestMapping(value={"/index"},method = RequestMethod.GET)
    public String userIndex(ModelMap modelMap){
        modelMap.addAttribute("user",new User());
        return "user.jsp";
    }

    @RequestMapping(value={"/singnUp"},method = RequestMethod.GET)
    public String singnUp(@Valid User user, BindingResult br, RedirectAttributes ra){
        ra.addFlashAttribute("user",user);
        return "user.jsp";
    }




}
